var map;         
var image = base_url+'assets/img/google/bed-icon-active.png';           
var service_url = base_url+lang+'/hotels/'+google_object_method+'/'
var infowindow;
var bounds; 
var markersArray=[];

var MAP_INITIALIZED = false;
var MARKER_ID = 0;
     
function initialize() {
    
    if(!LOADMAP)
        return;
        
    infowindow = new google.maps.InfoWindow({});
    bounds = new google.maps.LatLngBounds(); 
              
    var mapOptions = {
        center: new google.maps.LatLng(42.093828, 19.098272),
        zoom: 13,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        panControl: true,
        zoomControl: true,
        mapTypeControl: false,
        scaleControl: true,
        streetViewControl: false,
        overviewMapControl: false,
        scrollwheel: false


    };
                
    map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);  
   
    // go trough each hotel Array and add marker
    var cnt = hotelsArray.length;

    for(var i=0; i<cnt; i++){
        
        var latLang = new google.maps.LatLng(hotelsArray[i].x, hotelsArray[i].y);
        
        addMarker(
            latLang, 
            hotelsArray[i].name, 
            hotelsArray[i].ID
            );
                
        bounds.extend(latLang);
   
    }
    
    
    
    map.fitBounds(bounds);
    
    var listener = google.maps.event.addListener(map, "idle", function() { 
        if (map.getZoom() > 16) map.setZoom(16); 
        google.maps.event.removeListener(listener); 
    });
    
    function addMarker(location, title, hotelID) {
    
        marker = new google.maps.Marker({
            position: location,
            map: map,
            icon: image,
            title: title,
            hotspotid : hotelID
        });
    
        markersArray.push(marker);
        
        //log(title+": "+hotelID)
    
        // add an event listener for this marker
        bindInfoWindow(marker, map, infowindow);

    }

    function bindInfoWindow(marker, map, infowindow) { 
        google.maps.event.addListener(marker, 'click', function() { 
        
            $.ajax({
                url: service_url + marker.hotspotid+'?'+get_arr,
                dataType: 'json',
                success: open_info_window = function(data){

                    if(data.action==false && data.msg=='session_expired'){
                        window.location.reload();
                    }else{
                        infowindow.setContent(data.html);
                        infowindow.open(map, marker);
                    }
                }
            });
        
        }); 
    }
    
    cbHTML = $("#google_box").html();
    
    //console.log(MAP_INITIALIZED)
    
    if(MAP_INITIALIZED==false){
        
        MAP_INITIALIZED = true;
        
        $.ajax({
            url: service_url + markersArray[MARKER_ID].hotspotid+'?'+get_arr,
            dataType: 'json',
            success: open_info_window = function(data){

                if(data.action==false && data.msg=='session_expired'){
                    window.location.reload();
                }else{
                    infowindow.setContent(data.html);
                    infowindow.open(map, markersArray[0]);
                }
            }
        });
    }
    
    

}

$(document).ready(function() {
    
    if(LOADMAP){
     
        var offsetWinWidth = 100;
        var offsetWinHeight = 100;
    
        $('.GH').live('click',function(){
            var index = $(this).attr('data-holder');
            //log(index)

            $.ajax({
                url: service_url + markersArray[index].hotspotid+'?'+get_arr,
                dataType: 'json',
                success: open_info_window = function(data){

                    if(data.action==false && data.msg=='session_expired'){
                        window.location.reload();
                    }else{
                        infowindow.setContent(data.html);
                        infowindow.open(map, markersArray[0]);
                    }
                }
            });
               
        
        })
    
        gmpapW = $(window).innerWidth() - offsetWinWidth - 10;
        gmpapH = $(window).innerHeight() - offsetWinHeight;
    
        $("#google_map").css('width',gmpapW);
        $("#google_map").css('height',gmpapH);
    
        var cbHTML = $("#google_box").html();
        $("#google_box").remove();
    
        $(".googlebox").colorbox({
            html:"<div id='google_box'>"+cbHTML+"</div>",
            scrolling:false,
            width:$(window).innerWidth() - offsetWinWidth+"px",
            height:$(window).innerHeight() - offsetWinHeight+"px",
            onComplete:function(){

                if(MAP_INITIALIZED){
                    $.ajax({
                        url: service_url + markersArray[0].hotspotid+'?'+get_arr,
                        dataType: 'json',
                        success: open_info_window = function(data){

                            if(data.action==false && data.msg=='session_expired'){
                                window.location.reload();
                            }else{
                                infowindow.setContent(data.html);
                                infowindow.open(map, markersArray[0]);
                            }
                        }
                    });
                }else{
                    MARKER_ID = 0;
                }
                
                initialize();
            
            
            }
        });
    
        $('.show_map_object').colorbox({
            html:"<div id='google_box'>"+cbHTML+"</div>",
            scrolling:false,
            width:$(window).innerWidth() - offsetWinWidth+"px",
            height:$(window).innerHeight() - offsetWinHeight+"px",
            onComplete:function(){
            

                if(MAP_INITIALIZED){
                    $.ajax({
                        url: service_url + markersArray[0].hotspotid+'?'+get_arr,
                        dataType: 'json',
                        success: open_info_window = function(data){

                            if(data.action==false && data.msg=='session_expired'){
                                window.location.reload();
                            }else{
                                infowindow.setContent(data.html);
                                infowindow.open(map, markersArray[0]);
                            }
                        }
                    });
                }else{
                    MARKER_ID = 0;
                }
                
                initialize();
            
            }
        });
     
    }
    
    
    
});